
alter       trigger tft_msdept_bi on dbo.tft_msdept
instead of insert 
as   
begin
  set nocount on
  declare @nbalyear integer   
  declare @nbalmonth integer   
   
  declare @bmbala decimal(38,8)   
  declare @bysumdebit decimal(38,8)   
  declare @bysumcredit decimal(38,8)   
  declare @tmsumdebit decimal(38,8)   
  declare @tmsumcredit decimal(38,8)   
  declare @tmsum decimal(38,8)   
   
  declare @fbmbala decimal(38,8)   
  declare @fbysumdebit decimal(38,8)   
  declare @fbysumcredit decimal(38,8)   
  declare @ftmsumdebit decimal(38,8)   
  declare @ftmsumcredit decimal(38,8)   
  declare @ftmsum decimal(38,8)   
   

  declare 
    @deptid integer,   
    @subjectid integer,
    @nyear integer,   
    @nmonth integer,
    @filid integer,
    @moneyid integer

   
  declare local_cursor cursor local 
  for select deptid,subjectid,nyear,nmonth,filid,moneyid from inserted
  open local_cursor
  fetch next from local_cursor into @deptid,@subjectid,@nyear,@nmonth,@filid,@moneyid
  while @@fetch_status=0
  begin  
  set  @nbalyear=NULL
  set  @nbalmonth=NULL
  set  @bmbala = 0
  set  @bysumdebit = 0
  set  @bysumcredit = 0
  set  @tmsumdebit = 0
  set  @tmsumcredit = 0
  set  @tmsum = 0
  set  @fbmbala = 0
  set  @fbysumdebit = 0
  set  @fbysumcredit = 0
  set  @ftmsumdebit = 0
  set  @ftmsumcredit = 0
  set  @ftmsum = 0

    select @nbalyear=nbalyear,@nbalmonth=nbalmonth,@bmbala=bmbala,@bysumcredit=bysumcredit,@bysumdebit=bysumdebit,
      @tmsumcredit=tmsumcredit,@tmsumdebit=tmsumdebit,@tmsum=tmsum,
      @fbmbala=fbmbala,@fbysumcredit=fbysumcredit,@fbysumdebit=fbysumdebit,
      @ftmsumcredit=ftmsumcredit,@ftmsumdebit=ftmsumdebit,@ftmsum=ftmsum
    from dbo.f_msdeptgetpriorinfo(@moneyid,@deptid,@subjectid,@nyear,@nmonth,@filid)  
 
    set @bmbala = isnull(@bmbala,0)
    set @bysumcredit = isnull(@bysumcredit,0)
    set @bysumdebit = isnull(@bysumdebit,0)
    set @tmsumcredit = isnull(@tmsumcredit,0)
    set @tmsumdebit = isnull(@tmsumdebit,0)

    set @bysumcredit=@tmsumcredit + @bysumcredit
    set @bysumdebit =@tmsumdebit  + @bysumdebit

    set @bmbala=@bmbala+@tmsumdebit-@tmsumcredit

    set @fbmbala = isnull(@fbmbala,0)
    set @fbysumcredit = isnull(@fbysumcredit,0)
    set @fbysumdebit =isnull(@fbysumdebit,0)
    set @ftmsumcredit =isnull(@ftmsumcredit,0)
    set @ftmsumdebit =isnull(@ftmsumdebit,0)

    set @fbysumcredit=@ftmsumcredit + @fbysumcredit
    set @fbysumdebit =@ftmsumdebit  + @fbysumdebit

    set @fbmbala=@fbmbala+@ftmsumdebit-@ftmsumcredit

    if (@nyear<>@nbalyear)
    begin
      set @bysumcredit=0
      set @bysumdebit=0
      set @fbysumcredit=0
      set @fbysumdebit=0
    end
    

    insert tft_msdept(
      deptid,
      subjectid,
      nyear,
      nmonth,
      filid,
      moneyid,
      bmbala,
      bysumdebit,
      bysumcredit,
      tmsumdebit,
      tmsumcredit,
      tmsum,
      fbmbala,
      fbysumdebit,
      fbysumcredit,
      ftmsumdebit,
      ftmsumcredit,
      ftmsum,
      odata
    ) select 
      deptid,
      subjectid,
      nyear,
      nmonth,
      filid,
      isnull(@moneyid,0),      
      isnull(@bmbala,0),
      isnull(@bysumdebit,0),
      isnull(@bysumcredit,0),
      isnull(tmsumdebit,0),
      isnull(tmsumcredit,0),
      isnull(tmsum,0),
      isnull(@fbmbala,0),
      isnull(@fbysumdebit,0),
      isnull(@fbysumcredit,0),
      isnull(ftmsumdebit,0),
      isnull(ftmsumcredit,0),
      isnull(ftmsum,0),
      odata
    from inserted where deptid=@deptid and subjectid=@subjectid and nyear=@nyear and nmonth=@nmonth and moneyid=@moneyid
    fetch next from local_cursor into @deptid,@subjectid,@nyear,@nmonth,@filid,@moneyid
  end
  close local_cursor
  deallocate local_cursor
end



